package com.code.test.first.link;

/**
 * https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.md
 * <p>
 * 题意：反转一个单链表。
 * <p>
 * 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
 */
public class Code206 {

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode second = new ListNode(2);
        ListNode third = new ListNode(6);
        ListNode n4 = new ListNode(3);
        ListNode n5 = new ListNode(4);
        ListNode n6 = new ListNode(5);
        ListNode n7 = new ListNode(7);

        head.setNext(second);
        second.setNext(third);
        third.setNext(n4);
        n4.setNext(n5);
        n5.setNext(n6);
        n6.setNext(n7);


        ListNode tempHead = head;
        while (head != null) {
            System.out.print(head.getVal() + "→");
            head = head.getNext();
        }

        System.out.println("");
        ListNode reverse = reverseList(tempHead);

        while (reverse != null) {
            System.out.print(reverse.getVal() + "→");
            reverse = reverse.getNext();
        }

        String s=null;
        s.toLowerCase();
        System.out.println("");
    }

    public static ListNode reverseList(ListNode head) {

        ListNode cur = head;
        ListNode pre = null;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;

            cur = tmp;
        }

        return pre;

    }


    public static ListNode reverseLink(ListNode head) {
        if (head == null || head.getNext() == null) {
            return head;
        }
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode temp = cur.next;

            cur.next = pre;
            //往前移
            pre = cur;
            cur = temp;
        }

        return pre;
    }
}
